package com.zzq.my.jetpacks.jectpackapp.ui.stickyheaderrecyclerview

import by.kirich1409.viewbindingdelegate.viewBinding
import com.aisier.architecture.base.BaseActivity
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.databinding.ActivityStickyHeaderRvBinding
import tellh.com.stickyheaderview_rv.adapter.DataBean
import tellh.com.stickyheaderview_rv.adapter.StickyHeaderViewAdapter

/*
  * ================================================
  * 描述：RecyclerView 悬浮吸顶 Header，支持点击事件与状态绑定
  * 作者：zhuangzeqin
  * 时间: 2023/6/28 10:21
  * 邮箱：zzq@eeepay.cn
  * 备注:
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
  *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
  *
  * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
  * ----------------------------------------------------------------
  * ================================================
  */
class StickyHeaderRecyclerViewAct: BaseActivity(R.layout.activity_sticky_header_rv){
    private val vb by viewBinding(ActivityStickyHeaderRvBinding::bind)
    private lateinit var mStickyHeaderRecyclerAdapter:StickyHeaderRecyclerAdapter
    private lateinit var mRecyclerAdapter:RecyclerAdapter
    private lateinit var mStickyHeaderViewAdapter: StickyHeaderViewAdapter
    override fun initData() {

        mStickyHeaderViewAdapter = StickyHeaderViewAdapter(genDataList(0));
        mStickyHeaderViewAdapter.RegisterItemType(ItemHeaderViewBinder())
        mStickyHeaderViewAdapter.RegisterItemType(ItemViewBinder())
        vb.rvStickyHead.adapter = mStickyHeaderViewAdapter
//        mStickyHeaderRecyclerAdapter = StickyHeaderRecyclerAdapter()
//        mRecyclerAdapter = RecyclerAdapter()
//        mStickyHeaderRecyclerAdapter.setDataList(genDataList(0))
//        mRecyclerAdapter.setDataList(genDataList(0))

//        val layoutManager: StickyLinearLayoutManager = object : StickyLinearLayoutManager(this, mRecyclerAdapter) {
//                override fun isAutoMeasureEnabled(): Boolean {
//                    return true
//                }
//
//                override fun smoothScrollToPosition(
//                    recyclerView: RecyclerView,
//                    state: RecyclerView.State?,
//                    position: Int
//                ) {
//                    val smoothScroller: RecyclerView.SmoothScroller = TopSmoothScroller(recyclerView.context)
//                    smoothScroller.targetPosition = position
//                    startSmoothScroll(smoothScroller)
//                }
//
//                inner class TopSmoothScroller(context: Context?) :
//                    LinearSmoothScroller(context) {
//                    override fun calculateDtToFit(
//                        viewStart: Int,
//                        viewEnd: Int,
//                        boxStart: Int,
//                        boxEnd: Int,
//                        snapPreference: Int
//                    ): Int {
//                        return boxStart - viewStart
//                    }
//                }
//            }
//        layoutManager.elevateHeaders(5)
//        vb.rvStickyHead.layoutManager = layoutManager
//        vb.rvStickyHead.adapter = mRecyclerAdapter
//
//        layoutManager.setStickyHeaderListener(object : StickyHeaderListener {
//            override fun headerAttached(headerView: View, adapterPosition: Int) {
//                Log.d("StickyHeader", "Header Attached : $adapterPosition")
//            }
//
//            override fun headerDetached(headerView: View, adapterPosition: Int) {
//                Log.d("StickyHeader", "Header Detached : $adapterPosition")
//            }
//        })
//
//        vb.rvStickyHead.postDelayed(
//            Runnable { mRecyclerAdapter.addDataList(genDataList(mRecyclerAdapter.itemCount)) },
//            5000
//        )
    }

    override fun initView() {

    }

    override fun eventOnClick() {

    }

    /**
     * 测试数据
     */
    private fun genDataList(start: Int): List<DataBean> {
        val items: MutableList<DataBean> = ArrayList()
        for (i in start until 100 + start) {
            if (i % 10 == 0) {
                items.add(HeaderItem("Header $i"))
            } else {
                items.add(Item("Item $i", "description $i"))
                items.add(Item("Item $i", "description $i"))
            }
        }
        return items
    }
}